<template>
  <div>
    <div style="padding: 10px 0">
      <el-input style="width: 200px" placeholder="请输入评论人查询" suffix-icon="el-icon-search"
                v-model="uname"></el-input>
      <el-input style="width: 200px" placeholder="请输入评论帖子查询" suffix-icon="el-icon-search" class="ml-5"
                v-model="ptitle"></el-input>

      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="你确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
    </div>
    <el-table :data="tableData" border stripe
              :header-cell-class-name="'headerBg'"
              :header-cell-style="{'text-align':'center'}"
              :cell-style="{'text-align':'center'}" @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column prop="uname" label="评论人" width="150px">
      </el-table-column>
      <el-table-column prop="ctext" label="评论内容" width="500px">
      </el-table-column>
      <el-table-column prop="ptitle" label="评论帖子" width="180px">
      </el-table-column>
      <el-table-column prop="cdatetime" label="评论日期" width="180px">
      </el-table-column>
      <el-table-column label="处理操作">
        <template slot-scope="scope">
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="你确定删除吗？"
              @confirm="del(scope.row.cid)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "comment",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      uname: "",
      ptitle: "",
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request.get("/comment/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          uname: this.uname,
          ptitle: this.ptitle,
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.records
        this.total = res.total
      })

    },
    del(id) {
      this.request.delete("/comment/delete/" + id).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    delBatch() {
      let cids = this.multipleSelection.map(v => v.cid)
      this.request.post("/comment/del/batch", cids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // console.log(val)
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
  }
}
</script>
<style>
.headerBg {
  background-color: #eee !important;

}
</style>